package com.vercon.otamirror.dialog;

import static com.vercon.otamirror.OtaActivity.homeSettingViewModel;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.abupdate.iot_libs.data.remote.VersionInfo;
import com.vercon.commonmirror.Constants;
import com.vercon.commonmirror.cache.MirrorDataStorage;
import com.vercon.commonmirror.utils.LogUtils;
import com.vercon.otamirror.BuildConfig;
import com.vercon.otamirror.MainApplication;
import com.vercon.otamirror.R;
import com.vercon.otamirror.fragment.BaseFragment;
import com.vercon.otamirror.mode.FirmwareMode;
import com.vercon.otamirror.utils.helper.FragmentOpenManager;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by csh on 2021/4/13
 * 提示升级弹窗
 */
public class FirmwareUpdateDialog extends BaseFragment {

    @BindView(R.id.tv_old_version)
    TextView tvOldVersion;
    @BindView(R.id.tv_new_version)
    TextView tvNewVersion;
    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.btn_commit)
    TextView btnCommit;
    @BindView(R.id.btn_cancel)
    TextView btnCancel;
    @BindView(R.id.tv_up_content)
    TextView tvUpContent;

    public static boolean isShow = false;

    public FirmwareMode mUpdateMode;

    @Override
    protected int setContentView(@Nullable Bundle savedInstanceState) {
        return R.layout.fragment_firmware_update;
    }

    @Override
    protected void initView(View view) {
        isShow = true;
        Bundle arguments = getArguments();
        if (arguments != null) {
            mUpdateMode = (FirmwareMode) arguments.getSerializable(Constants.updateFirmware);
            LogUtils.e("mUpdateMode.toString()="+mUpdateMode.toString());
            FirmwareMode aa = MirrorDataStorage.instance().getObject(Constants.Firmware_Save, FirmwareMode.class);
            if (aa != null) {
                LogUtils.e("aa.toString()=" + aa);
            }
            if (mUpdateMode != null &&!TextUtils.isEmpty(mUpdateMode.newVersion) && mUpdateMode.releaseType) {
                MirrorDataStorage.instance().storeObject(Constants.Firmware_Save, mUpdateMode);
                LogUtils.e("mUpdateMode保存成功");
            }
            setUpData();
        }
        if (MainApplication.getContext().isForeground){
            MainApplication.getContext().setVirtualKey(false);
        }
    }

    public void setData(VersionInfo versionInfo) {
        //mUpdateMode = versionInfo;
        setUpData();
    }

    private void setUpData() {
        if (mUpdateMode == null) {
            return;
        }
        LogUtils.e(mUpdateMode.toString());
        tvOldVersion.setText( mUpdateMode.oldVersion);
        tvNewVersion.setText(mUpdateMode.newVersion);
        tv_content.setText(mUpdateMode.desc);
        if (mUpdateMode.releaseType) {
            //强制更新
            btnCancel.setVisibility(View.GONE);
            tvUpContent.setVisibility(View.VISIBLE);
            btnCommit.setText(getString(R.string.about_update));
        } else {
            btnCancel.setVisibility(View.VISIBLE);
            tvUpContent.setVisibility(View.GONE);
            btnCommit.setText(getString(R.string.app_update));
        }
    }

    @Override
    protected void initListener(View view) {

    }


    @OnClick({R.id.btn_cancel, R.id.btn_commit, R.id.tv_up_content})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                if (mUpdateMode != null) {
                    if (mUpdateMode.ispush) {
                        MirrorDataStorage.instance().storeBoolean(Constants.STORAGE_KEY_OTA, true);
                    }
                }
                onBack();
                homeSettingViewModel.setOtaSwitchData(true);
                break;
            case R.id.btn_commit:
                if (mUpdateMode != null) {
                    onBack();
                    //弹出下载进度页
                    UpdateFirmwareDialog updateDialog = new UpdateFirmwareDialog();
                    FragmentOpenManager.getInstance().addFragment(updateDialog,getActivity());
                }
                break;
            case R.id.tv_up_content:
                //调试用
                if (BuildConfig.DEBUG) {
                    onBack();
                }
                break;
        }
    }

    @Override
    public void onDestroyView() {

        isShow = false;
        super.onDestroyView();
    }
}